package com.example.websocket.redis.util;

import java.util.Date;

/**
 * 定义基本类型的枚举常用属性和基本操作
 * @author user
 *
 */
public enum BaseType {
	/**
	 * int
	 */
	IntegerType(Integer.class, Integer.TYPE, 0),
	/**
	 * String
	 */
	StringType(String.class, String.class, null), 
	/**
	 * boolean
	 */
	BooleanType(Boolean.class, Boolean.TYPE, true),
	/**
	 * long
	 */
	LongType(Long.class, Long.TYPE, 0),
	/**
	 * float
	 */
	FloatType(Float.class, Float.TYPE, 0.0),
	/**
	 * double
	 */
	DoubleType(Double.class, Double.TYPE, 0.0),
	/**
	 * Date
	 */
	DateType(Date.class, Date.class, null),
	/**
	 * byte
	 */
	ByteType(Byte.class, Byte.TYPE, 0),
	/**
	 * short
	 */
	ShortType(Short.class, Short.TYPE, 0),
	/**
	 * char
	 */
	CharacterType(Character.class, Character.TYPE, 0),
	/**
	 * void
	 */
	VoidType(Void.class, Void.TYPE, null),
	/**
	 * void
	 */
	OtherType(Object.class, Object.class, null);
	
	private BaseType(Class<?> type, Class<?> primitive, Object value) {
		this.type = type;
		this.primitive = primitive;
		this.value = value;
	}
	/**
	 * 通过类型获取配套的基本类型
	 * @param c 类型，需是各基本类型或者其子类
	 * @return 基本类型枚举
	 */
	public static BaseType classOf(Class<?> c){
		BaseType[] vs = values();
		for(int i = 0; i < vs.length; i++) {
			if(c.isPrimitive() && vs[i].getPrimitive().equals(c)){
				return vs[i];
			}else if(ClassUtils.isSuper(vs[i].type, c)){
				return vs[i];
			}
		}
		return OtherType;
	}
	private Class<?> type;
	private Class<?> primitive;
	private Object value;
	public Class<?> getType() {
		return type;
	}
	public void setType(Class<?> type) {
		this.type = type;
	}
	public Class<?> getPrimitive() {
		return primitive;
	}
	public void setPrimitive(Class<?> primitive) {
		this.primitive = primitive;
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
}
